
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
 --------------------------------------------------------------------------

 info7mpd 0.0.1 - Entzun albisteak MPD zerbitzarian!
 Xabi <xezeleta@gmail.com>

 --------------------------------------------------------------------------

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 --------------------------------------------------------------------------
"""
 
import os
import time
import datetime
import shutil
import urllib
import mpdclient2

class info7mpd:
	
	prefs = {
		'mpd_host': '10.15.180.74',
		'mpd_port': '6600',
	}
	
	i7url = 'http://www.info7.com/wp-content/uploads/albistegiak/boletinak/'
	mpddir = '/home/tknika/datuak/audio/Info7'
	current_hour = datetime.datetime.now().strftime("%H")
	if int(current_hour) < 16:
		# Goiza da, 12tako saioa jetsiko degu
		filename = datetime.datetime.now().strftime("%y%m%d")+"_1200ae.wav.mp3"
	else:
		# Arratsaldea da, 16tako saioa jetsiko degu
		filename = datetime.datetime.now().strftime("%y%m%d")+"_1600ae.wav.mp3"
	
	def deskargatu(self):
		h = urllib.urlretrieve(self.i7url+self.filename, self.mpddir+'/'+self.filename)
		print "Fitxategia: "+self.i7url+self.filename
		print "Fitxategia deskargatzen...		OK!"
		return True
	
	def mpdConnect(self):
		# Configurar las variables de entorno, si no existen
		os.environ['MPD_HOST'] = self.prefs['mpd_host']
		os.environ['MPD_PORT'] = self.prefs['mpd_port']
		
		#print 'MPD_HOST: '+os.environ['MPD_HOST']
		#print 'MPD_PORT: '+os.environ['MPD_PORT']
		
		try:
			self.m = mpdclient2.connect()
			#print "MPD zerbitzariarekin konektatzen...	OK!"
		except:
			return False
		return True
	
	def mpdUpdate(self):
		self.mpdConnect()
		self.m.do.update()
		print "MPD datubasea eguneratzen...		OK!"
		return True
	
	def mpdEnqFile(self):
		self.mpdConnect()
		dirfilename = self.m.do.search("filename", self.filename)
		dirfilename = dirfilename[0].file
		print "Aurkitutako emaitzak:"
		print dirfilename
		self.m.do.add(dirfilename)
		print "Fitxategia ilarara gehitzen...		OK!"
		self.playlistID=len(self.m.playlistinfo())-1
		return True
	
	def mpdPlayFile(self):
		self.mpdConnect()
		self.m.do.play(self.playlistID)
		print "Fitxategia erreproduzitzen...		OK!"
		return True
	
	def mpdDelFile(self):
		self.mpdConnect()
		self.m.do.delete(self.playlistID)
		print "Fitxategia ilaratik ezabatzen...	OK!"

	
	def __init__ (self):
		
		# Logerako
		print "======================"+datetime.datetime.now().strftime("%y/%m/%d - %H:%M")+"======================"

		# Deskargatu azken albisteak
		self.deskargatu()
		
		# Entzun azken albisteak
		if self.mpdConnect():
			# Eguneratu mpd datubasea
			self.mpdUpdate()
			time.sleep(30)	# TXAPU! Itxaron datubasea eguneratu arte
			
			# Gehitu ilarara
			self.mpdEnqFile()
			# Entzun!
			self.mpdPlayFile()

			# Ezabatu ilaratik
			time.sleep(360)	# TXAPU! Itxaron erreproduzitzen bukatu arte
			self.mpdDelFile()
		else:
			print "Ezin izan da mpd zerbitzarira konektatu!"
	
if __name__ == '__main__':
	info7mpd()
